﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TT.Module.sysLogin;
using SfcorpDLL.dao;
using SfcorpDLL.ety;
using System.Data;
using GT.ORM;

namespace SfcorpDLL.biz
{
     public class bizAccountDMT
    {
        etyBaeSystemParamter basSysParam;
        daoAccountDMT doAccountDMT;

        public bizAccountDMT(etyBaeSystemParamter ieyBasClassDeclare)
        {
            basSysParam = ieyBasClassDeclare;
            doAccountDMT = new daoAccountDMT(basSysParam);
        }
        
        //public IList<etyDmtCustomFblSummary> GetDmtCustomFblSum(etyDmtCustomFblSummary inEty)
        //{
        //    DataTable dt = doAccountDMT.GetDmtCustomFblSum(inEty);
        //    IList<etyDmtCustomFblSummary> ilRusult = TT.Common.Converts.ConvertToIList<etyDmtCustomFblSummary>(dt);
        //    return ilRusult;
        //}

        public IList<etyChargeDtl> GetBillResult(etyChargeDtl inEty)
        {
            DataTable dt = doAccountDMT.GetBillResult(inEty);
            IList<etyChargeDtl> ilRusult = TT.Common.Converts.ConvertToIList<etyChargeDtl>(dt);
            return ilRusult;
        }

        public DataTable GetDmtCustomFblSum(etyDmtCustomFblSummary inEty, ref string refAddColumns)
        {
            DataTable dtSum = doAccountDMT.GetDmtCustomFblSum(inEty);
            DataTable dtLoc = doAccountDMT.GetDmtCustomLocSum(inEty);
            DataColumn newDataCol;
            refAddColumns = string.Empty;
            string filter = string.Empty;
            string addColumns = string.Empty;
            string existColumn = "SEA_BILL_NO,AGENT_BILL_NO,CONTRACT_NO,CNTRS,LOC_AMOUNT";
            string addUpdateColumns = "CNTRS,LOC_AMOUNT";
            //插入字段
            foreach (DataColumn dc in dtLoc.Columns)
            {
                if (existColumn.IndexOf(dc.ColumnName) < 0)
                {
                    newDataCol = new DataColumn(dc.ColumnName, dc.DataType);
                    dtSum.Columns.Add(newDataCol);
                    addColumns += dc.ColumnName + ",";
                    refAddColumns += string.Format("[{0};{1}],", new string[] { dc.ColumnName, dc.ColumnName });
                }
            }
            refAddColumns = refAddColumns.TrimEnd(',');
            addUpdateColumns += "," + addColumns.TrimEnd(',');

            //更新数据
            foreach (DataRow drLoc in dtLoc.Rows)
            {
                filter = "JCONTRACT_NO='{0}' AND SEA_BILL_NO='{1}' ";//AND AGENT_BILL_NO='{2}'";
                filter = string.Format(filter, new string[] { 
                    drLoc["CONTRACT_NO"].ToString(),drLoc["SEA_BILL_NO"].ToString()});//,drLoc["AGENT_BILL_NO"].ToString()});
                DataRow[] drSum = dtSum.Select(filter);
                foreach (DataRow drSummary in drSum)
                {
                    foreach (string s in addUpdateColumns.Split(','))
                    {
                        drSummary[s] = drLoc[s];
                    }
                }
            }
            //加一列MATCH_FEE_AMOUNT_ID,是否金额匹配
            newDataCol = new DataColumn("MATCH_FEE_AMOUNT_BOOL", System.Type.GetType("System.Boolean"));
            dtSum.Columns.Add(newDataCol);
            foreach (DataRow dr in dtSum.Rows)
            {
                if (dr["MATCH_FEE_AMOUNT_ID"] == null || dr["MATCH_FEE_AMOUNT_ID"].ToString() == "0")
                {
                    dr["MATCH_FEE_AMOUNT_BOOL"] = false;
                    decimal dmtSummny=0;
                    decimal locSummny=0;
                    if(decimal.TryParse(dr["FEE_SUMMNY"].ToString(),out dmtSummny) && decimal.TryParse(dr["LOC_AMOUNT"].ToString(),out dmtSummny))
                    {
                        if (dmtSummny == locSummny)
                        {
                            dr["MATCH_FEE_AMOUNT_BOOL"] = true;
                        }
                    }
                }
                else
                {
                    dr["MATCH_FEE_AMOUNT_BOOL"] = true;
                }
            }
            return dtSum;
        }

        public IList<etyDmtCustomFblSummary> GetDmtCustomLocSum(etyDmtCustomFblSummary inEty)
        {
            DataTable dt = doAccountDMT.GetDmtCustomLocSum(inEty);
            IList<etyDmtCustomFblSummary> ilRusult = TT.Common.Converts.ConvertToIList<etyDmtCustomFblSummary>(dt);
            return ilRusult;
        }
    }
}